/*
 * redmicro all Copyright (c)
 */

/**
 * Created by seeker910 on 2015/1/6.
 */
Rsd.define('Rsd.Faith.admin.InitialPage', {
    extend: 'Rsd.container.Page',
    requires: ['Rsd.Faith.admin.SysBuildDialog','Rsd.control.ProgressBar'],
    layout:'vbox',
    items:[
        {
            style:{width:'100%',height:'90px'},
            layout:'hbox',
            items:[
                {
                    xtype:'label',
                    width:'20%',
                    height:50,
                    style:{ marginTop:15,fontSize:'120%',textAlign:'center',lineHeight:50,backgroundColor:'lightgreen'},
                    text:'第1步：新建属期'
                },
                {
                    xtype:'progress-bar',
                    width:'60%',
                    height:90,
                    value:100,
                    margin:'23 3% 29 3%'
                },
                {
                    xtype:'button',
                    width:'7%',
                    height:90,
                    margin:'20 2% 20 0%',
                    text:'执  行',
                    style:{fontSize:'120%'},
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                this.parent.parent.doExecute({group:'XJSQ',count:200});
                            }
                        }
                    },
                },{
                    xtype:'link',
                    width:'13%',
                    height:90,
                    margin:'10 0% 0 2%',
                    style:{lineHeight:25,marginTop:0},
                    text:'查看报告',
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                Rsd.widget('sys-build',{title:'新建属期',group:'XJSQ'}).showDialog();
                            }
                        }
                    }
                }
            ]
        },
        {
            style:{width:'100%',height:'90px'},
            layout:'hbox',
            items:[
                {
                    xtype:'label',
                    width:'20%',
                    height:50,
                    style:{ marginTop:15,fontSize:'120%',textAlign:'center',lineHeight:50,backgroundColor:'lightgreen'},
                    text:'第2步：清空数据库'
                },
                {
                    xtype:'progress-bar',
                    width:'60%',
                    height:90,
                    value:100,
                    margin:'23 3% 29 3%'
                },
                {
                    xtype:'button',
                    width:'7%',
                    height:90,
                    margin:'20 2% 20 0%',
                    text:'执  行',
                    style:{fontSize:'120%'},
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                this.parent.parent.doExecute({group:'QKSJ',count:200});
                            }
                        }
                    },
                },{
                    xtype:'link',
                    width:'13%',
                    height:90,
                    margin:'10 0% 0 2%',
                    style:{lineHeight:25,marginTop:0},
                    text:'查看报告',
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                Rsd.widget('sys-build',{title:'清空数据库',group:'QKSJ'}).showDialog();
                            }
                        }
                    }
                }
            ]
        },
        {
            style:{width:'100%',height:'90px'},
            layout:'hbox',
            items:[
                {
                    xtype:'label',
                    width:'20%',
                    height:50,
                    style:{ marginTop:15,fontSize:'120%',textAlign:'center',lineHeight:50,backgroundColor:'lightgreen'},
                    text:'第3步：数据迁移'
                },
                {
                    xtype:'progress-bar',
                    width:'60%',
                    height:90,
                    value:80,
                    margin:'23 3% 29 3%'
                },
                {
                    xtype:'button',
                    width:'7%',
                    height:90,
                    margin:'20 2% 20 0%',
                    text:'执  行',
                    style:{fontSize:'120%'},
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                this.parent.parent.doExecute({group:'SJQY',count:200});
                            }
                        }
                    },
                },{
                    xtype:'link',
                    width:'13%',
                    height:90,
                    margin:'10 0% 0 2%',
                    style:{lineHeight:25,marginTop:0},
                    text:'查看报告',
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                Rsd.widget('sys-build',{title:'数据迁移',group:'SJQY'}).showDialog();
                            }
                        }
                    }
                }
            ]
        },
        {
            style:{width:'100%',height:'90px'},
            layout:'hbox',
            items:[
                {
                    xtype:'label',
                    width:'20%',
                    height:50,
                    style:{ marginTop:15,fontSize:'120%',textAlign:'center',lineHeight:50,backgroundColor:'lightgreen'},
                    text:'第4步：数据整理'
                },
                {
                    xtype:'progress-bar',
                    width:'60%',
                    height:90,
                    margin:'23 3% 29 3%'
                },{
                    xtype:'button',
                    width:'7%',
                    height:90,
                    margin:'20 2% 20 0%',
                    text:'执  行',
                    style:{fontSize:'120%'},
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                this.parent.parent.doExecute({group:'SJZL',count:200});
                            }
                        }
                    },
                },{
                    xtype:'link',
                    width:'13%',
                    height:90,
                    margin:'10 0% 0 2%',
                    style:{lineHeight:25,marginTop:0},
                    text:'查看报告',
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                Rsd.widget('sys-build',{title:'数据整理',group:'SJZL'}).showDialog();
                            }
                        }
                    }
                }
            ]
        },
        {
            style:{width:'100%',height:'90px'},
            layout:'hbox',
            items:[
                {
                    xtype:'label',
                    width:'20%',
                    height:50,
                    style:{ marginTop:15,fontSize:'120%',textAlign:'center',lineHeight:50,backgroundColor:'lightgreen'},
                    text:'第5步：数据检查'
                },
                {
                    xtype:'progress-bar',
                    width:'60%',
                    height:90,
                    margin:'23 3% 29 3%'
                },{
                    xtype:'button',
                    width:'7%',
                    height:90,
                    margin:'20 2% 20 0%',
                    text:'执  行',
                    style:{fontSize:'120%'},
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                this.parent.parent.doExecute({group:'SJJC',count:200});
                            }
                        }
                    },
                },{
                    xtype:'link',
                    width:'13%',
                    height:90,
                    margin:'10 0% 0 2%',
                    style:{lineHeight:25,marginTop:0},
                    text:'查看报告',
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                Rsd.widget('sys-build',{title:'数据检查',group:'SJJC'}).showDialog();
                            }
                        }
                    }
                }
            ]
        },
        {
            style:{width:'100%',height:'90px'},
            layout:'hbox',
            items:[
                {
                    xtype:'label',
                    width:'20%',
                    height:50,
                    style:{ marginTop:15,fontSize:'120%',textAlign:'center',lineHeight:50,backgroundColor:'lightgreen'},
                    text:'第6步：基础数据写入'
                },
                {
                    xtype:'progress-bar',
                    width:'60%',
                    height:90,
                    margin:'23 3% 29 3%'
                },{
                    xtype:'button',
                    width:'7%',
                    height:90,
                    margin:'20 2% 20 0%',
                    text:'执  行',
                    style:{fontSize:'120%'},
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                this.parent.parent.doExecute({group:'JCSJ',count:200});
                            }
                        }
                    },
                },{
                    xtype:'link',
                    width:'13%',
                    height:90,
                    margin:'10 0% 0 2%',
                    style:{lineHeight:25,marginTop:0},
                    text:'查看报告',
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                Rsd.widget('sys-build',{title:'基础数据写入',group:'JCSJ'}).showDialog();
                            }
                        }
                    }
                }
            ]
        },
        {
            style:{width:'100%',height:'90px'},
            layout:'hbox',
            items:[
                {
                    xtype:'label',
                    width:'20%',
                    height:50,
                    style:{ marginTop:15,fontSize:'120%',textAlign:'center',lineHeight:50,backgroundColor:'lightgreen'},
                    text:'第7步：缴存入帐'
                },
                {
                    xtype:'progress-bar',
                    width:'60%',
                    height:90,
                    margin:'23 3% 29 3%'
                },{
                    xtype:'button',
                    width:'7%',
                    height:90,
                    margin:'20 2% 20 0%',
                    text:'执  行',
                    style:{fontSize:'120%'},
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                this.parent.parent.doExecute({group:'JCRZ',count:200});
                            }
                        }
                    },
                },{
                    xtype:'link',
                    width:'13%',
                    height:90,
                    margin:'10 0% 0 2%',
                    style:{lineHeight:25,marginTop:0},
                    text:'查看报告',
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                Rsd.widget('sys-build',{title:'缴存入帐',group:'JCRZ'}).showDialog();
                            }
                        }
                    }
                }
            ]
        },
        {
            style:{width:'100%',height:'90px'},
            layout:'hbox',
            items:[
                {
                    xtype:'label',
                    width:'20%',
                    height:50,
                    style:{ marginTop:15,fontSize:'120%',textAlign:'center',lineHeight:50,backgroundColor:'lightgreen'},
                    text:'第8步：分摊数据核查'
                },
                {
                    xtype:'progress-bar',
                    width:'60%',
                    height:90,
                    margin:'23 3% 29 3%'
                },{
                    xtype:'button',
                    width:'7%',
                    height:90,
                    margin:'20 2% 20 0%',
                    text:'执  行',
                    style:{fontSize:'120%'},
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                this.parent.parent.doExecute({group:'FTHC',count:200});
                            }
                        }
                    },
                },{
                    xtype:'link',
                    width:'13%',
                    height:90,
                    margin:'10 0% 0 2%',
                    style:{lineHeight:25,marginTop:0},
                    text:'查看报告',
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                Rsd.widget('sys-build',{title:'分摊数据核查',group:'FTHC'}).showDialog();
                            }
                        }
                    }
                }
            ]
        },
        {
            style:{width:'100%',height:'90px'},
            layout:'hbox',
            items:[
                {
                    xtype:'label',
                    width:'20%',
                    height:50,
                    style:{ marginTop:15,fontSize:'120%',textAlign:'center',lineHeight:50,backgroundColor:'lightgreen'},
                    text:'第9步：使用入帐'
                },
                {
                    xtype:'progress-bar',
                    width:'60%',
                    height:90,
                    margin:'23 3% 29 3%'
                },{
                    xtype:'button',
                    width:'7%',
                    height:90,
                    margin:'20 2% 20 0%',
                    text:'执  行',
                    style:{fontSize:'120%'},
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                this.parent.parent.doExecute({group:'SYRZ',count:200});
                            }
                        }
                    },
                },{
                    xtype:'link',
                    width:'13%',
                    height:90,
                    margin:'10 0% 0 2%',
                    style:{lineHeight:25,marginTop:0},
                    text:'查看报告',
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                Rsd.widget('sys-build',{title:'使用入帐',group:'SYRZ'}).showDialog();
                            }
                        }
                    }
                }
            ]
        },
        {
            style:{width:'100%',height:'90px'},
            layout:'hbox',
            items:[
                {
                    xtype:'label',
                    width:'20%',
                    height:50,
                    style:{ marginTop:15,fontSize:'120%',textAlign:'center',lineHeight:50,backgroundColor:'lightgreen'},
                    text:'第10步：资金总账账目按属期核算'
                },
                {
                    xtype:'progress-bar',
                    width:'60%',
                    height:90,
                    margin:'23 3% 29 3%'
                },{
                    xtype:'button',
                    width:'7%',
                    height:90,
                    margin:'20 2% 20 0%',
                    text:'执  行',
                    style:{fontSize:'120%'},
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                this.parent.parent.doExecute({group:'ZZHS',count:200});
                            }
                        }
                    },
                },{
                    xtype:'link',
                    width:'13%',
                    height:90,
                    margin:'10 0% 0 2%',
                    style:{lineHeight:25,marginTop:0},
                    text:'查看报告',
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                Rsd.widget('sys-build',{title:'资金总账账目按属期核算',group:'ZZHS'}).showDialog();
                            }
                        }
                    }
                }
            ]
        },
        {
            style:{width:'100%',height:'90px'},
            layout:'hbox',
            items:[
                {
                    xtype:'label',
                    width:'20%',
                    height:50,
                    style:{ marginTop:15,fontSize:'120%',textAlign:'center',lineHeight:50,backgroundColor:'lightgreen'},
                    text:'第11步：小区账目按属期核算'
                },
                {
                    xtype:'progress-bar',
                    width:'60%',
                    height:90,
                    margin:'23 3% 29 3%'
                },{
                    xtype:'button',
                    width:'7%',
                    height:90,
                    margin:'20 2% 20 0%',
                    text:'执  行',
                    style:{fontSize:'120%'},
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                this.parent.parent.doExecute({group:'XQHS',count:200});
                            }
                        }
                    },
                },{
                    xtype:'link',
                    width:'13%',
                    height:90,
                    margin:'10 0% 0 2%',
                    style:{lineHeight:25,marginTop:0},
                    text:'查看报告',
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                Rsd.widget('sys-build',{title:'小区账目按属期核算',group:'XQHS'}).showDialog();
                            }
                        }
                    }
                }
            ]
        },
        {
            style:{width:'100%',height:'90px'},
            layout:'hbox',
            items:[
                {
                    xtype:'label',
                    width:'20%',
                    height:50,
                    style:{ marginTop:15,fontSize:'120%',textAlign:'center',lineHeight:50,backgroundColor:'lightgreen'},
                    text:'第12步：小区楼栋账目按属期核算'
                },
                {
                    xtype:'progress-bar',
                    width:'60%',
                    height:90,
                    margin:'23 3% 29 3%'
                },{
                    xtype:'button',
                    width:'7%',
                    height:90,
                    margin:'20 2% 20 0%',
                    text:'执  行',
                    style:{fontSize:'120%'},
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                this.parent.parent.doExecute({group:'LDHS',count:200});
                            }
                        }
                    },
                },{
                    xtype:'link',
                    width:'13%',
                    height:90,
                    margin:'10 0% 0 2%',
                    style:{lineHeight:25,marginTop:0},
                    text:'查看报告',
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                Rsd.widget('sys-build',{title:'小区楼栋账目按属期核算',group:'LDHS'}).showDialog();
                            }
                        }
                    }
                }
            ]
        },
        {
            style:{width:'100%',height:'90px'},
            layout:'hbox',
            items:[
                {
                    xtype:'label',
                    width:'20%',
                    height:50,
                    style:{ marginTop:15,fontSize:'120%',textAlign:'center',lineHeight:50,backgroundColor:'lightgreen'},
                    text:'第13步：小区单元账目按属期核算'
                },
                {
                    xtype:'progress-bar',
                    width:'60%',
                    height:90,
                    margin:'23 3% 29 3%'
                },{
                    xtype:'button',
                    width:'7%',
                    height:90,
                    margin:'20 2% 20 0%',
                    text:'执  行',
                    style:{fontSize:'120%'},
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                this.parent.parent.doExecute({group:'DYHS',count:200});
                            }
                        }
                    },
                },{
                    xtype:'link',
                    width:'13%',
                    height:90,
                    margin:'10 0% 0 2%',
                    style:{lineHeight:25,marginTop:0},
                    text:'查看报告',
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                Rsd.widget('sys-build',{title:'小区单元账目按属期核算',group:'DYHS'}).showDialog();
                            }
                        }
                    }
                }
            ]
        },
        {
            style:{width:'100%',height:'90px'},
            layout:'hbox',
            items:[
                {
                    xtype:'label',
                    width:'20%',
                    height:50,
                    style:{ marginTop:15,fontSize:'120%',textAlign:'center',lineHeight:50,backgroundColor:'lightgreen'},
                    text:'第14步：分户账目按属期核算'
                },
                {
                    xtype:'progress-bar',
                    width:'60%',
                    height:90,
                    margin:'23 3% 29 3%'
                },{
                    xtype:'button',
                    width:'7%',
                    height:90,
                    margin:'20 2% 20 0%',
                    text:'执  行',
                    style:{fontSize:'120%'},
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                this.parent.parent.doExecute({group:'FHHS',count:200});
                            }
                        }
                    },
                },{
                    xtype:'link',
                    width:'13%',
                    height:90,
                    margin:'10 0% 0 2%',
                    style:{lineHeight:25,marginTop:0},
                    text:'查看报告',
                    listeners:{
                        'click':{
                            element:'ctrl',
                            fn:function (sender,event) {
                                Rsd.widget('sys-build',{title:'分户账目按属期核算',group:'FHHS'}).showDialog();
                            }
                        }
                    }
                }
            ]
        }
    ] ,
    /*
    * */
    constructor: function (config) {
        var me = this;
        config = config || {};
        Rsd.apply(this, config);
    },
    /*
    * */
    load:function load(){
        var me = this;
        Rsd.app.getService('System.GetTimerState').requestJson({},function (data) {

            if(data.success)
            {
                var timer = {
                    XJSQ:{index:0,total:0,enabled:0,success:0,fail:0},
                    QKSJ:{index:1,total:0,enabled:0,success:0,fail:0},
                    SJQY:{index:2,total:0,enabled:0,success:0,fail:0},
                    SJZL:{index:3,total:0,enabled:0,success:0,fail:0},
                    SJJC:{index:4,total:0,enabled:0,success:0,fail:0},
                    JCSJ:{index:5,total:0,enabled:0,success:0,fail:0},
                    JCRZ:{index:6,total:0,enabled:0,success:0,fail:0},
                    FTHC:{index:7,total:0,enabled:0,success:0,fail:0},
                    SYRZ:{index:8,total:0,enabled:0,success:0,fail:0},
                    ZZHS:{index:9,total:0,enabled:0,success:0,fail:0},
                    XQHS:{index:10,total:0,enabled:0,success:0,fail:0},
                    LDHS:{index:11,total:0,enabled:0,success:0,fail:0},
                    DYHS:{index:12,total:0,enabled:0,success:0,fail:0},
                    FHHS:{index:13,total:0,enabled:0,success:0,fail:0}
                };
                for(var i in data.data.report)
                {
                    //Count: 313, Group: "DYHS", Complete: 0, Success: 0
                     var item = data.data.report[i];

                    if(Rsd.isEmpty( timer[item.Group]))
                     {
                         continue;
                     }

                     timer[item.Group].total += item.Count;
                     if(item.Enabled==1)
                     {
                         timer[item.Group].enabled += item.Count||0;
                     }
                    if(item.Success==1)
                    {
                        timer[item.Group].success += item.Count||0;
                    }
                    if(item.Success==-1)
                    {
                        timer[item.Group].fail += item.Count||0;
                    }

                }
                for(var i in data.data.state)
                {
                    var item = data.data.state[i];
                    timer[item.group].status=item.status;
                }
                for(var i in timer)
                {
                    var item = timer[i];

                    if(Rsd.isEmpty(item) )
                    {
                        continue;
                    }

                    //console.log(i,item);

                    if(item.total==0)
                    {
                        me.items[item.index].items[1].setValue(100);
                        me.items[item.index].items[2].setDisabled(true);
                        me.items[item.index].items[2].setValue('无计划');
                        me.items[item.index].items[3].setText('创建计划');
                    }else
                    {
                        me.items[item.index].items[1].setValue((item.success+item.fail)*100/item.total);
                        me.items[item.index].items[2].setDisabled(item.enabled == item.total);
                        if((item.success + item.fail)== item.total)
                        {
                            me.items[item.index].items[2].setValue('已完成');
                        }
                        if(item.enabled < item.total)
                        {
                            me.items[item.index].items[2].setValue(item.status||'启动任务');
                        }
                        if((item.success + item.fail) < item.total && item.enabled == item.total)
                        {
                            me.items[item.index].items[2].setValue('正在执行');
                        }
                        if(item.success ==0 &&  item.fail==0  && item.enabled == item.total)
                        {
                            me.items[item.index].items[2].setValue('等待执行');
                        }

                        me.items[item.index].items[3].setText('启动：' + item.enabled + '/'+item.total +'（ ' +item.success +'成功 |'+ item.fail + '失败）' +'  点击查看详情');
                    }

                }

            }
        });
    },
    doExecute:function doExecute(args) {
        //
        var me = this;
        Rsd.app.getService('System.StartTimer').requestJson(args,function (data) {
            //console.log(data);
            if(data.success)
            {
                Rsd.alert('线程已启动：'+data.data)
            }
        });
    }
});
